package com.shangcheng.controller;

import com.shangcheng.common.Result;
import com.shangcheng.entity.TShopCart;
import com.shangcheng.entity.TShopCartView;
import com.shangcheng.service.OrderServiceImpl;
import com.shangcheng.service.ShopCartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopcartapi")
public class ShopCartController {
    @Autowired
    private ShopCartServiceImpl service;
    @Autowired
    private OrderServiceImpl orderService;
    //清空购物车
    @DeleteMapping("/{custmerId}/shopcarts")
    public Result delete(@PathVariable("custmerId") Integer custmerId) {
        service.clearShopByCustmerId(custmerId);
        return Result.ok("ok");
    }
    //购物车列表
    @GetMapping("/{custmerId}/shopcarts")
    public Result<List<TShopCartView>> findShopCarts(@PathVariable Integer custmerId) {
        return Result.ok(service.findShopCartViews(custmerId));
    }
    //添加购物车
    @PostMapping("/{custmerId}/shopcarts")
    public Result<TShopCart>  addShopCart(@PathVariable Integer custmerId, @RequestBody TShopCart shopCart) {
        shopCart.setCustmerId(custmerId);
        service.addShopCart(shopCart);
        return  Result.ok(shopCart);
    }
    //删除订单
    @DeleteMapping("/{custmerId}/shopcarts/{orderId}")
    public Result<Void> deleteOrder(@PathVariable("custmerId") Integer custmerId, @PathVariable("orderId") int orderId) {
        service.deleteShopCart(orderId);
        return Result.ok(null);
    }
    //删除用户购物车所有商品
    @DeleteMapping("/customers/{customerId}/shopcarts")
    public Result<Void> deleteShopCartByCustomerId(@PathVariable Integer customerId) {
        service.deleteShopCartByCus(customerId);
        return Result.ok(null);
    }
    //查询用户地址
    @GetMapping("/{customerId}/address")
    public String getAddressByCustomerId(@PathVariable Integer customerId) {
        return service.getAddressById(customerId);
    }
}
